package com.gdk.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
 
 
import org.apache.log4j.Logger;
 
 
/**
 * http请求工具类
 * @author chenp
 *
 */
public class HttpUtils {
 
    private final static int CONNECT_TIMEOUT = 5000; // in milliseconds  连接超时的时间
    private final static String DEFAULT_ENCODING = "UTF-8";  //字符串编码
    private static Logger lg=Logger.getLogger(HttpUtils.class);
    
    public static String postData(String urlStr, String data) throws Exception{  
        return postData(urlStr, data, "application/json;charset=utf-8");  
    }  
    /**
     * post数据请求
     * @param urlStr
     * @param data
     * @param contentType
     * @return
     * @throws Exception 
     */
    public static String postData(String urlStr, String data, String contentType) throws Exception{  
        BufferedReader reader = null;  
        /*try { */
        	System.out.println("执行开始");
        	System.out.println("urlStr : "+urlStr);
            URL url = new URL(urlStr);  
            URLConnection conn = url.openConnection();  
            conn.setDoOutput(true);  
            conn.setConnectTimeout(CONNECT_TIMEOUT);  
            conn.setReadTimeout(CONNECT_TIMEOUT);  
            if(contentType != null)  
                conn.setRequestProperty("content-type", contentType);  
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);  
            System.out.println("data : "+data);
            if(data == null) {
                System.out.println("data == null ");
            	data = "";              	
            }  
            System.out.println("data ！= null ");
            writer.write(data);   
            System.out.println("写入data ");
            writer.flush();  
            writer.close();    
            System.out.println("中程");
            InputStream inputStream = conn.getInputStream();
            System.out.println("inputStream : "+inputStream);
            InputStreamReader streamReader = new InputStreamReader(inputStream,DEFAULT_ENCODING);
            System.out.println("streamReader : "+streamReader);
            reader = new BufferedReader(streamReader);  
          
            System.out.println("连接正常");
            StringBuilder sb = new StringBuilder();  
            String line = null;  
            while ((line = reader.readLine()) != null) {  
                sb.append(line);  
                sb.append("\r\n");  
            }  
            
            System.out.println("sb.toString() : "+sb.toString());
            
            
            return sb.toString();  
   /*     } catch (IOException e) {  
        lg.info("Error connecting to " + urlStr + ": " + e.getMessage());  
        } finally {  
            try {  
                if (reader != null)  
                    reader.close();  
            } catch (IOException e) {  
            }  
        }  
        return null;  */
    }  
}